package com.xiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyang.annotations.OperationLog;
import com.xiangyang.entity.Employee;
import com.xiangyang.entity.dto.Result;
import com.xiangyang.entity.vo.EmployeeAndDepartment;
import com.xiangyang.entity.vo.EmployeeLike;
import com.xiangyang.mapper.IEmployeeMapper;
import com.xiangyang.service.IEmployeeServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmployeeServiceImpl extends ServiceImpl<IEmployeeMapper, Employee> implements IEmployeeServer {
    @Autowired
    private IEmployeeMapper ui;
@OperationLog(title = "添加医生", businessType = "新增")
    @Override
    public Result addDoctor(Employee employee) {
        if (employee.getEmployeeNumber() == null
                || employee.getUserName() == null
                || employee.getUserSex() == null
                || employee.getUserAge() == null
                || employee.getPhone() == null
                || employee.getExperience() == null
                || employee.getPosts() == null
                || employee.getGrade() == null
                || employee.getDepartmentId() == null
                || employee.getIdCard() == null
                || employee.getAddress() == null
        ) {
            return Result.error("输入有误");
        }
        int insert = ui.insert(employee);
        if (insert != 1) {
            return Result.error("添加失败");
        }
        return Result.ok(employee);
    }
@OperationLog(title = "删除医生", businessType = "删除医生", oldDataMethod = "getDoctor")
    @Override
    public Result deleteDoctor(Long employeeId) {

        int doctor = ui.deleteById(employeeId);
        if (doctor != 1) {
            return Result.error("删除失败");
        }
        return Result.ok(employeeId);
    }
    public Employee getDoctor(Long employeeId) {
        return ui.selectById(employeeId);
    }
@OperationLog(title = "修改医生", businessType = "修改医生", oldDataMethod = "getDoctorList", newDataMethod = "updateDoctor")
    @Override
    public Result updateDoctor(Employee employee) {
        int i = ui.updateById(employee);
        if (i != 1) {
            return Result.error("修改失败");
        }
        return Result.ok(employee);
    }

    @Override
    public Result getDoctorList() {
        return Result.ok(ui.selectList(null));

    }

    @Override
    public Result  getDoctorLists(Employee doctor) {
//        Map<String, Object> map = new HashMap<>();
//        if (doctor.getUserName()!=null)
//            map.put("user_name", doctor.getUserName());
//        if(doctor.getUserSex()!=null)
//            map.put("user_sex", doctor.getUserSex());
//        if(doctor.getEmployeeNumber()!=null)
//            map.put("employee_number", doctor.getEmployeeNumber());
//        map.put("phone", doctor.getPhone());
//        map.put("experience", doctor.getExperience());
//        map.put("department_id", doctor.getDepartmentId());
//        map.put("posts", doctor.getPosts());
//        map.put("grade", doctor.getGrade());
//         List<Employee> employees = ui.selectByMap(map);
        QueryWrapper<Employee> qw = new QueryWrapper<>();
//        qw.likeRight(1);
        ui.selectList(qw);
        return Result.ok(qw);
    }
    @Override
   public Result getemployeeList(Employee doctor) {
        QueryWrapper<Employee> qw = new QueryWrapper<>();
        if (doctor.getUserName()!=null)
        qw.likeRight("user_name",doctor.getUserName());
        if (doctor.getUserSex()!=null)
        qw.likeRight("user_sex",doctor.getUserSex());
        if (doctor.getEmployeeNumber()!=null)
        qw.likeRight("employee_number",doctor.getEmployeeNumber());
        if (doctor.getPhone()!=null)
        qw.likeRight("phone",doctor.getPhone());
       if (doctor.getExperience()!=null);
        qw.likeRight("experience",doctor.getExperience());
        if (doctor.getDepartmentId()!=null)
        qw.likeRight("department_id",doctor.getDepartmentId());
         if (doctor.getPosts()!=null)
        qw.likeRight("posts",doctor.getPosts());
         if (doctor.getGrade()!=null)
        qw.likeRight("grade",doctor.getGrade());
        return Result.ok(ui.selectList(qw));
    }

    @Override
    public Result selectEmployee(EmployeeLike employee) {
        Page<Employee> page = new Page<>(employee.getPage(),employee.getSize());
       LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.like(Employee::getUserName,employee.getUserName());
        qw.like(Employee::getPosts,employee.getPosts());
        return  Result.ok(ui.selectPage(page,qw));
    }




}

